/**  
* Copyright (c) 2008, Noel Lynch All rights reserved. Redistribution and use  
* in source and binary forms, with or without modification, are permitted  
* provided that the following conditions are met: Redistributions of source  
* code must retain the above copyright notice, this list of conditions and the  
* following disclaimer. Redistributions in binary form must reproduce the above  
* copyright notice, this list of conditions and the following disclaimer in the  
* documentation and/or other materials provided with the distribution. Neither  
* the name of Noel Lynch nor the names of contributors may be used to  
* endorse or promote products derived from this software without specific prior  
* written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
*/
// Aug 6, 2008

package com.sceneworker.editors.spatialtransformereditor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;

import com.acarter.scenemonitor.propertydescriptor.editor.A_EditorPanel.I_ComponentModificationCallback;
import com.jme.animation.SpatialTransformer;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.sceneworker.editors.rotationeditor.EulerQuaternionEditorPanel;
import com.sceneworker.util.swing.QuaternionEditorStandAlone;
import com.sceneworker.util.swing.Vector3fEditorPanelStandAlone;


public class PointInTimeEditor extends JPanel implements I_ComponentModificationCallback {
	
	private		SpatialTransformer.PointInTime		mcl_ptInTime;
	private		SpatialTransformer					mcl_transformer;
	private		int									mi_objIndex;
	
	private		Vector3fEditorPanelStandAlone		mcl_tx;
	private		Vector3fEditorPanelStandAlone		mcl_scale;
	private		EulerQuaternionEditorPanel			mcl_rot;
	
	public
	PointInTimeEditor(SpatialTransformer cl_par, int i_objIndex) {
		super();
		
		mi_objIndex = i_objIndex;
		mcl_transformer = cl_par;
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		buildPanel();
	}
	
	public	void	setPtInTime(SpatialTransformer.PointInTime cl_ptInTime) {
		mcl_ptInTime = cl_ptInTime;
		initEditors();
	}
	
	private	void	initEditors() {
		if(mcl_ptInTime.usedTrans.get(0)) {
			mcl_tx.setVector(mcl_ptInTime.look[0].getTranslation());
		} else {
			mcl_tx.setVector(new Vector3f(0, 0, 0));
		}
		
		if(mcl_ptInTime.usedRot.get(0)) {
			mcl_rot.setQuaternion(mcl_ptInTime.look[0].getRotation());
		} else {
			mcl_rot.setQuaternion(new Quaternion());
		}
		
		if(mcl_ptInTime.usedScale.get(0)) {
			mcl_scale.setVector(mcl_ptInTime.look[0].getScale());
		} else {
			mcl_scale.setVector(new Vector3f(1, 1, 1));
		}
	}
	
	private	void
	buildPanel() {
		mcl_tx = new Vector3fEditorPanelStandAlone(new Vector3f());
		mcl_tx.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Translation"),
                BorderFactory.createEmptyBorder(5,5,5,5)));

		mcl_scale = new Vector3fEditorPanelStandAlone(new Vector3f(1, 1, 1));
		mcl_scale.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Scale"),
                BorderFactory.createEmptyBorder(5,5,5,5)));
		
		
		mcl_rot = new EulerQuaternionEditorPanel(new Quaternion(), false, new EulerQuaternionEditorPanel.IRotationModifiedCallback() {
			public void rotationModifed(EulerQuaternionEditorPanel cl_rot) {
				componentModifiedCallback();
			}
			
		});
		mcl_rot.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Rotation"),
                BorderFactory.createEmptyBorder(5,5,5,5)));
		
		mcl_tx.setCallback(this);
		mcl_scale.setCallback(this);
		
		JPanel		lcl_panel = new JPanel();
		lcl_panel.setLayout(new BoxLayout(lcl_panel, BoxLayout.Y_AXIS));
		lcl_panel.add(mcl_tx);
		lcl_panel.add(mcl_rot);
		lcl_panel.add(mcl_scale);
		
		add(lcl_panel);
	}

	public void componentModifiedCallback() {
		if(mcl_ptInTime != null) {
			mcl_transformer.setRotation(mi_objIndex, mcl_ptInTime.time, mcl_rot.getQuaternion());
			mcl_transformer.setPosition(mi_objIndex, mcl_ptInTime.time, mcl_tx.getVector());
			mcl_transformer.setScale(mi_objIndex, mcl_ptInTime.time, mcl_scale.getVector());
			
			mcl_transformer.update(0);
		}
		
	}
}